package everyday;

public class T1053 {
    public int[] prevPermOpt1(int[] arr) {
        int len=arr.length;
        int left=len-1,right=len-1;
        for (int i = len-1; i >0 ; i--) {
            if (i==0)return arr;
            if (arr[i]<arr[i-1]){
                left=i-1;
                break;
            }
        }
        for (int i = len-1; i >left ; i--) {
            if (arr[i]<arr[left]){
                while (arr[i]==arr[i-1])i--;
                right=i;
                break;
            }
        }
        int temp=arr[left];
        arr[left]=arr[right];
        arr[right]=temp;
        return arr;
    }
}
